package com.lnlyj.media.ui;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.lnlyj.media.R;
import com.lnlyj.media.base.BaseAdapter;
import com.lnlyj.media.data.MediaItem;

import androidx.annotation.NonNull;

/**
 * wanglei 2020-04-20
 */
public class AlbumMediaAdapter extends BaseAdapter<MediaItem, AlbumMediaHolder> {

    @Override
    public AlbumMediaHolder getViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_media, parent, false);
        return new AlbumMediaHolder(view);
    }

    @Override
    public void bindData(AlbumMediaHolder holder, int position) {
        MediaItem item =getItem(position);
        Glide.with(holder.imageView.getContext()).load(item.getUri()).skipMemoryCache(false).crossFade(0).into(holder.imageView);

        if (item.isVideo()) {
            holder.playView.setVisibility(View.VISIBLE);
        } else {
            holder.playView.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(item, position);
            }
        });


        holder.playView.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(item.getUri(), "video/*");
            holder.playView.getContext().startActivity(intent);
        });

    }

    public int getIndex(MediaItem item) {
        return getList().indexOf(item);
    }
}
